<script setup lang="ts">
defineOptions({name:'detailPage'})
import {ref,onMounted} from 'vue'
import {getArticleDetail,collect} from '../../api/article'
import type {CustomItem,likeCollect} from '../../types/data'
import { useRoute} from 'vue-router'
const route=useRoute()
const IdParams = ref({id:route.query.id})

const articleDetail=ref<CustomItem>({
    id:'',
    stem:'',
    content:'',
    createdAt:'',
    creator:'',
    avatar:'',
    likeCount:0,
    views:0,
})
//获取详情
const getDetail=async()=>{
    const res=await getArticleDetail(IdParams.value)
    articleDetail.value=res.data
}
const collectParams=ref<likeCollect>({
  id:'',
  optType:1
})

//点赞
const toggleLike=async ()=>{
  collectParams.value.id=route.query.id as string
  collectParams.value.optType=1
  console.log(collectParams.value);
  await collect(collectParams.value)

    console.log('点赞');
    
}

//收藏
const toggleCollect= async()=>{
  collectParams.value.id=route.query.id as string
  collectParams.value.optType=2
  console.log(collectParams.value);
  await collect(collectParams.value)
    console.log('收藏');
    
}

onMounted(()=>{
    getDetail()
})
</script>
<template>
    <div class="detail-page">
      <van-nav-bar
        left-text="返回"
        left-arrow
        fixed
        @click-left="$router.back()"
        title="标题"
      />
      <div style="width: 100%;height: 50px;"></div>
      <header class="header">
        <h1>{{ articleDetail.stem }}</h1>
        <p>{{ articleDetail.createdAt }} | {{ articleDetail.views }} 浏览量 | {{ articleDetail.likeCount }} 点赞数
        </p>
        <p class="img-title">
          <img :src="articleDetail.avatar" alt="" />
          <span>{{ articleDetail.creator }}</span>
        </p>
      </header>
      <main class="body" v-html="articleDetail.content"></main>
      <div class="opt">
        <van-icon @click="toggleLike"  name="like-o"/>
        <van-icon @click="toggleCollect"  name="star-o"/>
      </div>
    </div>
  </template>

<style lang="scss" scoped>

.detail-page{
    padding:0 20px;
 
}
.header{
    h1{
        font-size: 24px;
    }
    p{
        font-size: 14px;
        color: #999;
    }
    .img-title{
        display: flex;
        align-items: center;
        img{
            width: 50px;
            margin: 0 10px;
        }
    }
}
.opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
</style>